package com.wheel.autoconfigure.web.mvc.filter;/*
 * <h3>Title:wheel-parent系统</h3>
 * <p>Copyright (c) 2022, wnfbx@163.com. All rights reserved.</p>
 * @author wnfbx@163.com
 */

import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public abstract class CacheExceptionOncePerRequestFilter extends OncePerRequestFilter implements Filter {

    public static final String EXCEPTION_KEY = "FILTER.EXCEPTION";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        try {
            this.doFilter(request, response, filterChain);
        } catch (Exception e) {
            request.setAttribute(EXCEPTION_KEY, e);
            request.getRequestDispatcher(BaseErrorController.BASE_ERROR_CONTROLLER_REQUEST_MAPPING).forward(request, response);
        }
        filterChain.doFilter(request, response);
    }

    protected abstract void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws Exception;
}
